<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/10/24
 * Time: 14:25
 * Comment: ''
 */

namespace App\Repository;

use App\Models\MerchantsInfoApplyFor;

class MerchantsInfoApplyForRepo extends BaseRepo
{
    /**
     * 根据手机号获取信息
     * @param string $phone
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-24 14:26
     */
    public function getByPhone(string $phone)
    {
        return MerchantsInfoApplyFor::query()->where('contact_phone',$phone)->orderBy('create_time','desc')->first();
    }

    /**
     * 根据分渠道标识获取信息
     * @param string $assort_link
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-24 14:45
     */
    public function getAssortLink(string $assort_link)
    {
        return MerchantsInfoApplyFor::query()->where('assort_link',$assort_link)->orderBy('create_time','desc')->first();
    }

    /**
     * 保存数据
     * @param array $data
     * @param string $apply_for_num
     * @return int
     * @author shen.zk
     * @date 2023-10-24 15:19
     */
    public function save(array $data, string $apply_for_num = '')
    {
        if (!empty($apply_for_num))
        {
            return MerchantsInfoApplyFor::query()->where('apply_for_num',$apply_for_num)->update($data);
        }else{
            return MerchantsInfoApplyFor::query()->insertGetId($data);
        }
    }

    /**
     * 根据入驻申请流水号获取信息
     * @param string $apply_for_num
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-24 18:11
     */
    public function getByApplyForNum(string $apply_for_num)
    {
        return MerchantsInfoApplyFor::query()->where('apply_for_num',$apply_for_num)->first();
    }

    /**
     * 根据接入商户号获取信息
     * @param string $apply_for_mid
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-24 18:28
     */
    public function getByApplyForMid(string $apply_for_mid)
    {
        return MerchantsInfoApplyFor::query()->where('apply_for_mid',$apply_for_mid)->first();
    }

    /**
     * 根据接入商户号获取信息
     * @param string $apply_for_mid
     * @return array
     * Author:xsc
     * Time:2023/11/22 15:42
     */
    public function getInfoByMid(string $apply_for_mid)
    {
        $res = MerchantsInfoApplyFor::query()->where('apply_for_mid', $apply_for_mid)->first();
        return $res ? $res->toArray() : [];
    }

    /**
     * 根据id获取信息
     * @param int $id
     * @return array
     * Author:xsc
     * Time:2023/11/22 16:25
     */
    public function getInfoById(int $id)
    {
        $res = MerchantsInfoApplyFor::query()->where('id', $id)->first();
        return $res ? $res->toArray() : [];
    }

}
